package com.yunti.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunti.springboot.mapper.*;
import com.yunti.springboot.po.*;
import com.yunti.springboot.service.ExamDetailService;
import com.yunti.springboot.util.SemesterUtil;
import com.yunti.springboot.vo.Result;
import com.yunti.springboot.vo.req.GetAllExamDetailReq;
import com.yunti.springboot.vo.res.AllExamDetailByClassRes;
import com.yunti.springboot.vo.res.StudentWithSubScoreRes;
import com.yunti.springboot.vo.res.SubItemRes;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
public class ExamDetailServiceImpl implements ExamDetailService {

    @Resource
    private SubjectMapper subjectMapper;

    @Resource
    private ElectiveSubMapper electiveSubMapper;

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private OptionMapper optionMapper;

    @Resource
    private ScoreMapper scoreMapper;

    @Resource
    private SubItemMapper subItemMapper;

//    @Override
//    public Result getAllExamDetailByClassId(GetAllExamDetailReq getAllExamDetailReq) {
//        Subject subject = subjectMapper.selectById(getAllExamDetailReq.getSubId());
//        if (subject == null) {
//            return Result.error("找不到项目");
//        }
//
//        //如果查询学期为null,则表明查询当前学期
//        if (getAllExamDetailReq.getSemester() == null) {
//            getAllExamDetailReq.setSemester(SemesterUtil.getNowSemesterByYear(getAllExamDetailReq.getYear()));
//        }
//
//        //得到该学期考试科目
//        LambdaQueryWrapper<ElectiveSub> electiveSubLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        electiveSubLambdaQueryWrapper.eq(ElectiveSub::getSemester, getAllExamDetailReq.getSemester());
//        List<ElectiveSub> electiveSubList = electiveSubMapper.selectList(electiveSubLambdaQueryWrapper);
//
//        //得到选考项的id
//        List<Integer> selectSubElectiveIdList = electiveSubList.stream().filter(e -> Objects.equals(e.getSubId(), getAllExamDetailReq.getSubId()) || (Objects.equals(e.getType(), subject.getType()) && Objects.equals(e.getType(), subject.getType()) && Objects.equals(e.getType(), subject.getType()))).map(ElectiveSub::getId).collect(Collectors.toList());
//
//        List<Student> studentList = new ArrayList<>();
//        List<Integer> stuIdList = new ArrayList<>();
//        //体考项目的必选和体制健康，那就是全部学生
//        if (subject.getType() == 1 || subject.getType() == 4) {
//            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            studentLambdaQueryWrapper.eq(Student::getClassId, getAllExamDetailReq.getClassId());
//            studentList = studentMapper.selectList(studentLambdaQueryWrapper);
//            stuIdList = studentList.stream().map(Student::getId).collect(Collectors.toList());
//        } else {
//            LambdaQueryWrapper<Option> optionLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            optionLambdaQueryWrapper.in(Option::getElectiveSubId, selectSubElectiveIdList);
//            optionLambdaQueryWrapper.in(Option::getSubId, getAllExamDetailReq.getSubId());
//            List<Option> optionList = optionMapper.selectList(optionLambdaQueryWrapper);
//            stuIdList = optionList.stream().map(Option::getStuId).collect(Collectors.toList());
//            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            studentLambdaQueryWrapper.in(Student::getId, stuIdList);
//            studentList = studentMapper.selectList(studentLambdaQueryWrapper);
//        }
//
//        //获取分数
//        LambdaQueryWrapper<Score> scoreLambdaQueryWrapper = new LambdaQueryWrapper<Score>();
//        scoreLambdaQueryWrapper.in(Score::getStuId, stuIdList);
//        scoreLambdaQueryWrapper.eq(Score::getSubId, getAllExamDetailReq.getSubId());
//        scoreLambdaQueryWrapper.eq(Score::getElectiveId,selectSubElectiveId);
//        List<Score> scoreList = scoreMapper.selectList(scoreLambdaQueryWrapper);
//
//        LambdaQueryWrapper<SubItem> subItemLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        subItemLambdaQueryWrapper.eq(SubItem::getSubId, getAllExamDetailReq.getSubId());
//        List<SubItem> subItemList = subItemMapper.selectList(subItemLambdaQueryWrapper);
//        Map<Integer, List<SubItem>> subItemListMap = subItemList.stream().collect(Collectors.groupingBy(SubItem::getSubId));
//
//        List<StudentWithSubScoreRes> studentWithSubScoreResList = new ArrayList<>();
//        for (int i = 0; i < studentList.size(); i++) {
//            StudentWithSubScoreRes studentWithSubScoreRes = new StudentWithSubScoreRes();
//            Student student = studentList.get(i);
//            BeanUtils.copyProperties(student, studentWithSubScoreRes);
//            studentWithSubScoreRes.setElectiveSubId(selectSubElectiveId);
//
//            //设置科目名
//            //获取子项目,并过滤性别
//            List<SubItem> childSubItemList = subItemListMap.get(subject.getId()).stream().filter(e -> !e.getItemNo().equals(0) && (e.getSextype().equals(0) || e.getSextype().equals(student.getSex()))).collect(Collectors.toList());
//            List<SubItemRes> subItemResList = new ArrayList<>();
//            for (int j = 0; j < childSubItemList.size(); j++) {
//                SubItemRes subItemRes = new SubItemRes();
//                BeanUtils.copyProperties(childSubItemList.get(j), subItemRes);
//
//                int finalJ = j;
//                List<Score> childScoreList = scoreList.stream().filter(new Predicate<Score>() {
//                    @Override
//                    public boolean test(Score score) {
//                        return !score.getItemNo().equals(0) && score.getItemNo().equals(childSubItemList.get(finalJ).getItemNo()) && score.getStuId().equals(student.getId());
//                    }
//                }).collect(Collectors.toList());
//
//                subItemRes.setScoreList(childScoreList);
//                subItemResList.add(subItemRes);
//
//            }
//
//            SubItemRes topSubItemRes = new SubItemRes();
//            List<SubItem> topSubItemList = subItemListMap.get(subject.getId()).stream().filter(e -> e.getItemNo().equals(0) && (e.getSextype().equals(0) || e.getSextype().equals(student.getSex()))).collect(Collectors.toList());
//            if (topSubItemList.size() > 0) {
//                BeanUtils.copyProperties(topSubItemList.get(0), topSubItemRes);
//            }
//
//            topSubItemRes.setScoreList(scoreList.stream().filter(new Predicate<Score>() {
//                @Override
//                public boolean test(Score score) {
//                    return score.getItemNo().equals(0) && score.getStuId().equals(student.getId());
//                }
//            }).collect(Collectors.toList()));
//
//            studentWithSubScoreRes.setTopSubItemRes(topSubItemRes);
//            studentWithSubScoreRes.setSubItemList(subItemResList);
//            studentWithSubScoreResList.add(studentWithSubScoreRes);
//        }
//
//        AllExamDetailByClassRes allExamDetailByClassRes = new AllExamDetailByClassRes();
//        allExamDetailByClassRes.setStudentWithSubScoreResList(studentWithSubScoreResList);
//
//        return Result.success(allExamDetailByClassRes);
//    }

    @Override
    public Result getAllExamDetailByClassId(GetAllExamDetailReq getAllExamDetailReq) {
        Subject subject = subjectMapper.selectById(getAllExamDetailReq.getSubId());
        if (subject == null) {
            return Result.error("找不到项目");
        }

        //如果查询学期为null,则表明查询当前学期
        if (getAllExamDetailReq.getSemester() == null) {
            getAllExamDetailReq.setSemester(SemesterUtil.getNowSemesterByYear(getAllExamDetailReq.getYear()));
        }

        //得到该学期考试科目
        LambdaQueryWrapper<ElectiveSub> electiveSubLambdaQueryWrapper = new LambdaQueryWrapper<>();
        electiveSubLambdaQueryWrapper.eq(ElectiveSub::getSemester, getAllExamDetailReq.getSemester());
        List<ElectiveSub> electiveSubList = electiveSubMapper.selectList(electiveSubLambdaQueryWrapper);

        //得到选考项的id
        List<Integer> selectSubElectiveIdList = electiveSubList.stream().filter(e -> Objects.equals(e.getSubId(), getAllExamDetailReq.getSubId()) || (!Objects.equals(e.getType(), 1) && !Objects.equals(e.getType(), 4) && Objects.equals(e.getType(), subject.getType()))).map(ElectiveSub::getId).collect(Collectors.toList());

        //体考项目的必选和体制健康，那就是全部学生
        List<Student> studentList = new ArrayList<>();
        List<Integer> stuIdList = new ArrayList<>();
        Map<Integer, Option> optionMap = new HashMap<>();
        if (subject.getType() == 1 || subject.getType() == 4) {
            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            studentLambdaQueryWrapper.eq(Student::getClassId, getAllExamDetailReq.getClassId());
            studentList = studentMapper.selectList(studentLambdaQueryWrapper);
            stuIdList = studentList.stream().map(Student::getId).collect(Collectors.toList());
        } else {
            //获取选了本科目的学生
            LambdaQueryWrapper<Option> optionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            optionLambdaQueryWrapper.in(Option::getElectiveSubId, selectSubElectiveIdList);
            optionLambdaQueryWrapper.in(Option::getSubId, getAllExamDetailReq.getSubId());
            List<Option> optionList = optionMapper.selectList(optionLambdaQueryWrapper);
            optionMap = optionList.stream().collect(Collectors.toMap(Option::getStuId, Function.identity(), (k1, k2) -> k1));

            stuIdList = optionList.stream().map(Option::getStuId).collect(Collectors.toList());
            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            studentLambdaQueryWrapper.in(Student::getId, stuIdList);
            studentList = studentMapper.selectList(studentLambdaQueryWrapper);
        }

        //获取分数
        LambdaQueryWrapper<Score> scoreLambdaQueryWrapper = new LambdaQueryWrapper<Score>();
        scoreLambdaQueryWrapper.in(Score::getStuId, stuIdList);
        scoreLambdaQueryWrapper.eq(Score::getSubId, getAllExamDetailReq.getSubId());
        scoreLambdaQueryWrapper.in(Score::getElectiveId, selectSubElectiveIdList);
        List<Score> scoreList = scoreMapper.selectList(scoreLambdaQueryWrapper);

        LambdaQueryWrapper<SubItem> subItemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        subItemLambdaQueryWrapper.eq(SubItem::getSubId, getAllExamDetailReq.getSubId());
        List<SubItem> subItemList = subItemMapper.selectList(subItemLambdaQueryWrapper);
        Map<Integer, List<SubItem>> subItemListMap = subItemList.stream().collect(Collectors.groupingBy(SubItem::getSubId));

        List<StudentWithSubScoreRes> studentWithSubScoreResList = new ArrayList<>();
        for (int i = 0; i < studentList.size(); i++) {
            StudentWithSubScoreRes studentWithSubScoreRes = new StudentWithSubScoreRes();
            BeanUtils.copyProperties(studentList.get(i), studentWithSubScoreRes);
            if (optionMap.get(studentWithSubScoreRes.getId()) != null) {
                studentWithSubScoreRes.setElectiveSubId(optionMap.get(studentWithSubScoreRes.getId()).getElectiveSubId());
            }else{
                studentWithSubScoreRes.setElectiveSubId(selectSubElectiveIdList.get(0));
            }
            //设置科目名
            //获取子项目,并过滤性别
            List<SubItem> childSubItemList = subItemListMap.get(subject.getId()).stream().filter(e -> !e.getItemNo().equals(0) && (e.getSextype().equals(0) || e.getSextype().equals(studentWithSubScoreRes.getSex()))).collect(Collectors.toList());
            List<SubItemRes> subItemResList = new ArrayList<>();
            for (int j = 0; j < childSubItemList.size(); j++) {
                SubItemRes subItemRes = new SubItemRes();
                BeanUtils.copyProperties(childSubItemList.get(j), subItemRes);

                int finalJ = j;
                List<Score> childScoreList = scoreList.stream().filter(new Predicate<Score>() {
                    @Override
                    public boolean test(Score score) {
                        return !score.getItemNo().equals(0) && score.getItemNo().equals(childSubItemList.get(finalJ).getItemNo()) && score.getStuId().equals(studentWithSubScoreRes.getId());
                    }
                }).collect(Collectors.toList());

                subItemRes.setScoreList(childScoreList);
                subItemResList.add(subItemRes);

            }

            SubItemRes topSubItemRes = new SubItemRes();
            List<SubItem> topSubItemList = subItemListMap.get(subject.getId()).stream().filter(e -> e.getItemNo().equals(0) && (e.getSextype().equals(0) || e.getSextype().equals(studentWithSubScoreRes.getSex()))).collect(Collectors.toList());
            if (topSubItemList.size() > 0) {
                BeanUtils.copyProperties(topSubItemList.get(0), topSubItemRes);
            }

            topSubItemRes.setScoreList(scoreList.stream().filter(new Predicate<Score>() {
                @Override
                public boolean test(Score score) {
                    return score.getItemNo().equals(0) && score.getStuId().equals(studentWithSubScoreRes.getId());
                }
            }).collect(Collectors.toList()));

            studentWithSubScoreRes.setTopSubItemRes(topSubItemRes);
            studentWithSubScoreRes.setSubItemList(subItemResList);
            studentWithSubScoreResList.add(studentWithSubScoreRes);
        }

        AllExamDetailByClassRes allExamDetailByClassRes = new AllExamDetailByClassRes();
        allExamDetailByClassRes.setStudentWithSubScoreResList(studentWithSubScoreResList);
        return Result.success(allExamDetailByClassRes);
    }

}
